package com.cnc.fd;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.cnc.bas.util.Log;
import com.cnc.bas.util.SpringBeanFactory;
import com.cnc.fd.service.FdDao;

/**
 * 系统参数
 * 
 * @author wujg
 * 
 */
public class SysConfig {
    
    // CDN名称
    public static final String CDN_NAME = "CDN_NAME";
    
    // 系统通知邮件列表
    public static final String CODE_NAME_MAIL_LIST = "SYS_CONFIG_MAIL_LIST";
    
    // 网易魔兽定义的CDN编号
    public static final String CODE_NAME_WOW_CDN_CODE = "WOW_CDN_CODE";
    
    // 网易魔兽定义的CDN名称
    public static final String CODE_NAME_WOW_CDN_NAME = "WOW_CDN_NAME";
    
    // 网易魔兽反馈接口
    public static final String CODE_NAME_WOW_FEEDBACK_URL = "WOW_FEEDBACK_URL";
    
    // 网易客户名称
    public static final String CODE_NAME_WOW_ENTER_NAME = "WOW_ENTER_NAME";
    
    // 客户反馈线程数
    public static final String FTP_FEEDBACK_THREAD_NUMBER = "FTP_FEEDBACK_THREAD_NUMBER";
    
     // 客户反馈用户名
    public static final String WOW_FTP_FEEDBACK_AUTH_NAME = "WOW_FTP_FEEDBACK_AUTH_NAME";
    
    // 客户反馈密码
    public static final String WOW_FTP_FEEDBACK_AUTH_PASSWORD = "WOW_FTP_FEEDBACK_AUTH_PASSWORD";
    
    // CP 推送失败通知的人员邮箱
    public static final String WSCP_PURGE_FAILURE_NOTICE_MAILS = "WSCP_PURGE_FAILURE_NOTICE_MAILS";
    
    // CP 推送地址
    public static final String WSCP_ADDRESS = "WSCP_ADDRESS";
    
    // 是否需要判断源文件是否存在
    public static final String PURGE_TO_CP_URL_CHECK = "PURGE_TO_CP_URL_CHECK";
    
    // release提交的URL
    public static final String RELEASE_SUBMIT_URL = "RELEASE_SUBMIT_URL";
    
    // 只能使用wsftp传输的客户
    public static final String CUSTOMER_WSFTP_ONLY = "CUSTOMER_WSFTP_ONLY";
    
    // 只能使用http传输的客户
    public static final String CUSTOMER_HTTP_ONLY = "CUSTOMER_HTTP_ONLY";
    
    // 跨运营商HTTP是否中转
    public static final String DIFF_ISP_USE_HAPROXY = "DIFF_ISP_USE_HAPROXY";
    
    // 跨运营商默认使用的下载方式
    public static final String DIFF_ISP_DEFAULT_DOWNLOAD_PROTOCOL = "DIFF_ISP_DEFAULT_DOWNLOAD_PROTOCOL";
    
    // 相同运营商之间互传是http下载线程数
    public static final String HTTPDOWNLOAD_THREADS_SAMEISP = "HTTPDOWNLOAD_THREADS_SAMEISP";
    // 不同运营商之间互传是http下载线程数
    public static final String HTTPDOWNLOAD_THREADS_DIFFISP = "HTTPDOWNLOAD_THREADS_DIFFISP";

    // http允许发送进程数
    public static final String MAX_HTTP_UPLOAD = "MAX_HTTP_UPLOAD";
    
    // wsftp允许发送进程数
    public static final String MAX_WSFTP_UPLOAD = "MAX_WSFTP_UPLOAD";
    
    // wsftp允许接受进程数
    public static final String MAX_WSFTP_RECEIVE = "MAX_WSFTP_RECEIVE";
    
    // 反馈重试的次数
    public static final String FEEDBACK_MAX_SEND_TIME = "FEEDBACK_MAX_SEND_TIME";
    
    // 反馈失败允许的最高失败次数（当天）
    public static final String FEEDBACK_MAX_FAIL_COUNT_BY_ONE_DATE = "FEEDBACK_MAX_FAIL_COUNT_BY_ONE_DATE";
    
    // 反馈线程执行的时间间隔（单位：秒）
    public static final String FEEDBACK_EXEC_INTERVAL = "FEEDBACK_EXEC_INTERVAL";
    
    // 其他任务的超时时间
    public static final String DEFAULT_OTHER_TASK_TIMEOUT_MINUTES = "DEFAULT_OTHER_TASK_TIMEOUT_MINUTES";
    
    // 接收任务允许的最大时间间隔
    public static final String DEFAULT_MAX_OVER_TIME_INTERVAL = "DEFAULT_MAX_OVER_TIME_INTERVAL";
    
    
    private Log log = Log.getLogger(SysConfig.class);

    private static SysConfig instance = new SysConfig();

    // 存放系统参数
    private Map<String, String> sysConfig = new HashMap<String, String>();

    /**
     * 根据参数名称获取系统参数值
     * 
     * @param code
     * @return
     */
    public static String getValue(String code) {
        return instance.getValueByCode(code);
    }
    
    /**
     * 根据参数名称获取系统参数值
     * @param code
     * @param defaultValue
     * @return
     */
    public static String getValue(String code, String defaultValue) {
        return instance.getValueByCode(code, defaultValue);
    }
    
    /**
     * 根据参数名称获取系统参数值
     * 
     * @param code
     * @return
     */
    public static int getIntValue(String code, int defaultValue) {
        String value = instance.getValueByCode(code);
        if (!StringUtils.isEmpty(value)) {
            try {
                return Integer.parseInt(value);
            } catch (Exception e) {
            }
        }

        return defaultValue;
    }
    
    /**
     * 刷新系统参数
     */
    public static void refresh(){
         instance.refreshSysConfig();
    }

    /**
     * 根据参数名称获取系统参数值
     * 
     * @param code
     * @return
     */
    public String getValueByCode(String code) {
        synchronized (sysConfig) {
            return sysConfig.get(code);
        }
    }
    
    /**
     * 获取值
     * @param code
     * @param defaultValue
     * @return
     */
    public String getValueByCode(String code, String defaultValue) {
        synchronized (sysConfig) {
            if(sysConfig.containsKey(code)){
                return sysConfig.get(code);  
            }
        }
        return defaultValue;
    }

    private SysConfig() {
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        FdDao fdDao = (FdDao) SpringBeanFactory.getBean(FdDao.BEAN_ID);
        List sysConfigList = fdDao.querySysConfig();
        if (sysConfigList != null && !sysConfigList.isEmpty()) {
            synchronized (sysConfig) {
                for (int i = 0; i < sysConfigList.size(); i++) {
                    Map mailConfig = (Map) sysConfigList.get(i);
                    sysConfig.put((String) mailConfig.get("PARAM_CODE"),
                            (String) mailConfig.get("PARAM_VALUE"));
                }
            }
        }
    }

    /**
     * 刷新用户邮件配置信息
     */
    public void refreshSysConfig() {
        log.logDebug("refreshUserMailConfig begin.");
        FdDao fdDao = (FdDao) SpringBeanFactory.getBean(FdDao.BEAN_ID);
        List sysConfigList = fdDao.querySysConfig();
        if (sysConfigList == null || sysConfigList.isEmpty()) {
            // 没有邮件配置，则清空
            sysConfig.clear();
            log.logDebug("sysConfig is empty.");
            return;
        }

        synchronized (sysConfig) {
            sysConfig.clear();
            for (int i = 0; i < sysConfigList.size(); i++) {
                Map mailConfig = (Map) sysConfigList.get(i);
                sysConfig.put((String) mailConfig.get("PARAM_CODE"),
                        (String) mailConfig.get("PARAM_VALUE"));
            }
        }
    }

}
